/* 
 * File:   RealConverter.cpp
 * Author: Gab'z
 * 
 * Created on 4 juin 2012, 09:40
 */

#include "RealConverter.h"
#include "../../../Context.h"
#include "RealConverter.h"
#include "../../literal/Complex.h"

#include "model/command/literal/Real.h"

namespace calculator {

    RealConverter::RealConverter(const std::string name) : UnaryOperator(name) {
    }

    RealConverter::RealConverter(const RealConverter& orig) : UnaryOperator(orig._name) {
    }

    RealConverter::~RealConverter() {
    }

    std::string RealConverter::isExecutable() const {
        if (Context::getInstance()->getCurrentStack()->size() == 0) return "Empty stack";
        return "";
    }

    RealConverter* RealConverter::clone() const {
        return new RealConverter(_name);
    }

    const Number* RealConverter::apply(const Number* number) const throw (ArithmeticException) {
        const Complex* complex = dynamic_cast<const Complex*> (number);
        const SimpleNumber* sn = dynamic_cast<const SimpleNumber*> (number);
        if (complex != 0) return new Real(complex->getRe()->value());
        else if (sn != 0) return new Real(sn->value());
        throw ArithmeticException("NaN");
    }
}